﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Net.Http;
using System.Net;
using System.Text;

namespace MGXAPI.Filter
{
    public class PermissionFilterAttribute : AuthorizeAttribute
    {
        /*
        /// <summary>
        /// Action 执行前执行,验证是否已经登录
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            try
            {

                var result = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), false);
                if (result)
                {
                    return;
                }
                var token = SessionHelper.Get(SessionKey.TokenKeyName)?.ToString();
                var backUrl = $"{HttpContext.Current.Request.Url.Scheme}://{HttpContext.Current.Request.Url.Authority}";
                if (string.IsNullOrWhiteSpace(token))
                {
                    if (HttpContext.Current.Request.QueryString["Code"] != null)
                    {
                        var url = $"{Utilities.WebApiUrl()}/api/PassPort/GetToken";
                        var tokenInfo = new Common.HttpHelper().PostJson<object, Result<TokenInfo>>(url, new { Code = HttpContext.Current.Request.QueryString["Code"], AppId = Utilities.AppId() });
                        if (tokenInfo == null || !tokenInfo.IsSuccess || tokenInfo.Data == null || string.IsNullOrEmpty(tokenInfo.Data.Token))
                        {
                            filterContext.Result = new RedirectResult("/Error.html");
                            return;
                        }
                        SessionHelper.SetSession(SessionKey.TokenKeyName, tokenInfo.Data.Token);
                        filterContext.Result = new RedirectResult("/Home");
                        return;
                    }
                    filterContext.Result = new RedirectResult($"{Utilities.SSOUrl()}/PassPort/Index?backUrl={HttpContext.Current.Server.UrlEncode(backUrl)}");
                    return;
                }

                if (!VerifyToken(token))
                {
                    SessionHelper.Clear();
                    filterContext.Result = new RedirectResult($"{Utilities.SSOUrl()}/PassPort/Index?backUrl={HttpContext.Current.Server.UrlEncode(backUrl)}");
                    return;
                }

            }
            catch (Exception ex)
            {
                SessionHelper.Clear();

                filterContext.Result = new RedirectResult("/Error.html");
                return;
            }


        }

        public bool VerifyToken(string token)
        {
            var url = $"{Utilities.WebApiUrl()}/api/PassPort/VerifyToken";
            var vToken = new Common.HttpHelper().PostJson<object, Result<string>>(url, new { Token = token, AppId = Utilities.AppId() });
            if (vToken == null || !vToken.IsSuccess)
            {
                return false;
            }
            return true;
        }
        private bool CheckIsChangeUser(string userName)
        {
            if (HttpContext.Current.Session[SessionKey.Current_User] == null)
                return true;
            try
            {
                UserInfoModel model = HttpContext.Current.Session[SessionKey.Current_User] as UserInfoModel;
                if (model.UserLoginID != userName)
                    return true;
            }
            catch (Exception)
            {
                return true;
            }

            return false;
        }
        */
    }
}